package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.PlatformResult;
import com.zito.health.entity.Expert;
import com.zito.health.service.ExpertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 专家表
 */
@Slf4j
@RestController
@RequestMapping("/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    @PostMapping("/save")
    public PlatformResult save(Expert item){
        expertService.save(item);
        return PlatformResult.success();
    }

    @PostMapping("/update")
    public PlatformResult update(Expert item){
        expertService.update(item);
        return PlatformResult.success();
    }

    @PostMapping("/query")
    public PlatformResult query(Integer id){
        return PlatformResult.success(expertService.queryById(id));
    }

    @PostMapping("/list")
    public PlatformResult query(Expert item, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        if(pageNo!=null&&pageSize!=null){
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Expert> list = expertService.query(item);
        PageInfo<Expert> pageInfo = new PageInfo<>(list);
        PageResult<Expert> result = new PageResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(pageInfo.getList());
        return PlatformResult.success(result);
    }

}
